package vip.xiaonuo.smzq.modular.dictionary.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import vip.xiaonuo.common.pojo.CommonResult;
import vip.xiaonuo.smzq.core.gson.GsonUtil;
import vip.xiaonuo.smzq.core.tools.excel.ExcelImportUtil;
import vip.xiaonuo.smzq.modular.dictionary.entity.Cb02Kslx;
import vip.xiaonuo.smzq.modular.dictionary.entity.Cb03Kspj;
import vip.xiaonuo.smzq.modular.dictionary.entity.excelEnt.Cb03KspjExc;
import vip.xiaonuo.smzq.modular.dictionary.service.ICb03KspjService;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;


/**
 * 矿石品级
 *
 * @author eomer
 * @since 2024-02-21
 */
@Api(tags = "矿石品级")
@RestController
@RequestMapping("/dictionary/cb03Kspj")
@Validated
public class Cb03KspjController {

    @Resource
    private ICb03KspjService iCb03KspjService;

    /**
     * 添加矿石品级
     */
    @ApiOperation("添加矿石品级")
    @PostMapping("/add")
    public CommonResult<String> add(@RequestBody @Valid Cb03Kspj cb03Kspj) {
        iCb03KspjService.save(cb03Kspj);
        return CommonResult.ok();
    }

    /**
     * 删除矿石品级
     */
    @ApiOperation("删除矿石品级")
    @PostMapping("/delete")
    public CommonResult<String> delete(@ApiParam(name = "pkid", value = "主键") @RequestParam String pkid) {
        iCb03KspjService.removeById(pkid);
        return CommonResult.ok();
    }

    /**
     * 编辑矿石品级
     */
    @ApiOperation("编辑矿石品级")
    @PostMapping("/edit")
    public CommonResult<String> edit(@RequestBody @Valid Cb03Kspj cb03Kspj) {
        iCb03KspjService.updateById(cb03Kspj);
        return CommonResult.ok();
    }

    /**
     * 矿石品级分页查询
     */
    @ApiOperation("矿石品级分页查询")
    @GetMapping("/list")
    public CommonResult<Page<Cb03Kspj>> page() {
        return CommonResult.data(iCb03KspjService.page(new Cb03Kspj()));
    }

    /**
     * 矿产品级筛选下拉
     * kcdm 矿产代码
     */
    @ApiOperation("矿产品级筛选下拉")
    @GetMapping("/kcpjList")
    public CommonResult<List<Cb03Kspj>> kslxList(Integer kcdm) {
        return CommonResult.data(iCb03KspjService.kcpjList(kcdm));
    }

    /**
     * 导入
     */
    @ApiOperation("导入")
    @PostMapping("/import")
    public CommonResult<String> importExcel(@RequestParam("file") MultipartFile file) {
        try {
            List<Cb03KspjExc> list = ExcelImportUtil.readMultipartFile(file, Cb03KspjExc.class);
            List<Cb03Kspj> cb01Zhs = GsonUtil.ObjectToList(list, Cb03Kspj.class);
            iCb03KspjService.saveBatch(cb01Zhs);
            return CommonResult.ok();
        } catch (Exception e) {
            return CommonResult.error(e.getMessage());
        }
    }
}
